using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Voodoo.Sauce.Internal.Extension;

namespace Voodoo.Analytics
{
	internal static class AnalyticsUtil
	{
		private const string TAG = "Analytics - Util";

		private const string CONTEXT_VAR_REGEXP_PATTERN = "^[a-z_]+$";

		internal static string ConvertDictionaryToJson(Dictionary<string, object> eventCustomData, string dataJson = null, string customVariables = null, string contextVariables = null)
		{
			Dictionary<string, object> dictionary = eventCustomData.RemoveNullValues();
			if (dataJson != null)
			{
				dictionary.Add("data", JsonConvert.DeserializeObject(dataJson));
			}
			if (customVariables != null)
			{
				dictionary.Add("cvars", JsonConvert.DeserializeObject(customVariables));
			}
			if (contextVariables != null)
			{
				dictionary.Add("context", JsonConvert.DeserializeObject(contextVariables));
			}
			return JsonConvert.SerializeObject(dictionary);
		}

		internal static string ConvertDictionaryToCustomVarJson(Dictionary<string, object> eventCustomVariables)
		{
			Dictionary<string, object> dictionary = new Dictionary<string, object>();
			int num = 0;
			foreach (KeyValuePair<string, object> eventCustomVariable in eventCustomVariables)
			{
				dictionary.Add($"c{num}_key", eventCustomVariable.Key);
				dictionary.Add($"c{num}_val", eventCustomVariable.Value?.ToString() ?? "");
				num++;
			}
			return JsonConvert.SerializeObject(dictionary);
		}

		internal static string ConvertDictionaryToContextVarJson(Dictionary<string, object> eventContextVariables)
		{
			Dictionary<string, object> dictionary = new Dictionary<string, object>();
			foreach (KeyValuePair<string, object> eventContextVariable in eventContextVariables)
			{
				string text = eventContextVariable.Key.FormatKeyName();
				if (!Regex.IsMatch(text, "^[a-z_]+$"))
				{
					Exception exception = new Exception("Context Property name '" + eventContextVariable.Key + "' on VoodooAnalytics event is invalid. Property name must match pattern \"^[a-z_]+$\"");
					AnalyticsLog.CustomLoggerLogE("Analytics - Util", exception);
					AnalyticsLog.CustomLoggerReportE(exception);
					continue;
				}
				object obj = CheckValue(eventContextVariable.Value);
				if (obj != null)
				{
					dictionary.Add(text, obj);
				}
			}
			return JsonConvert.SerializeObject(dictionary);
		}

		private static object CheckValue(object obj)
		{
			if (obj != null)
			{
				if (obj is string || obj is char || obj is byte || obj is decimal || obj is int || obj is long || obj is sbyte || obj is short || obj is uint || obj is ulong || obj is ushort || obj is bool || obj is float || obj is double)
				{
					return obj;
				}
				Exception exception = new Exception("Context Property value on VoodooAnalytics event must not be an object of type " + obj.GetType().FullName);
				AnalyticsLog.CustomLoggerLogE("Analytics - Util", exception);
				AnalyticsLog.CustomLoggerReportE(exception);
				return null;
			}
			return null;
		}
	}
}
